package com.wtb.listener;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wtb.domain.Config;
import com.wtb.pub.WtbException;

/**
 * 
 * 初始化API信息
 * @author tacheno
 *
 */
public class ApiInitListener implements ServletContextListener {
	private static final Log log = LogFactory.getLog(ApiInitListener.class);
	public void contextDestroyed(ServletContextEvent sce) {

	}
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext context = sce.getServletContext();
		Properties prop = new Properties();
		InputStream inputStream = null;
		try {
		inputStream = context
				.getResourceAsStream("WEB-INF/classes/apiInit.properties");
			prop.load(inputStream);
			Config.setRrConfig(prop.getProperty("rr_api_url"), prop
					.getProperty("rr_oauth_url"), prop
					.getProperty("rr_token_url"), prop
					.getProperty("rr_app_key"), prop
					.getProperty("rr_app_secret"), prop
					.getProperty("rr_redirect_url"));
			log.info("人人API信息初始化成功！！！");
			Config.setQqConfig(prop.getProperty("qq_api_url"), prop
					.getProperty("qq_oauth_url"), prop
					.getProperty("qq_token_url"), prop
					.getProperty("qq_app_key"), prop
					.getProperty("qq_app_secret"), prop
					.getProperty("qq_redirect_url"));
			log.info("腾讯API信息初始化成功！！！");
		} catch (IOException e) {
			log.error("API信息初始化失败：", e);
			throw new WtbException("API信息初始化失败",e);
		}finally{
			try {
				inputStream.close();
			} catch (IOException e) {
				log.error("API信息数据流关闭失败：", e);
			}
		}

	}
}
